<template>
  <div class="search">
    <el-form ref="queryFormRef" :model="courseName" :inline="true">
      <el-form-item label="关键字" prop="name">
        <el-input v-model="courseName" placeholder="课程名" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="getAllInfo"><i-ep-search />搜索</el-button>
        <el-button @click="resetQuery">
          <i-ep-refresh />
          重置</el-button
        >
      </el-form-item>
    </el-form>
  </div>
  <div>
    <el-card
      ><el-button type="primary" round @click="addCourse"
        >添加课程信息</el-button
      ></el-card
    >
    <el-table :data="courseData" style="width: 100%">
      <el-table-column prop="id" label="课程编号" width="150" />
      <el-table-column prop="courseName" label="课程名称" width="150" />
      <el-table-column prop="startWeek" label="开始周" width="150" />
      <el-table-column prop="endWeek" label="结束周" width="150" />
      <el-table-column prop="remain" label="剩余容量" width="100" />
      <el-table-column prop="examTime" label="考试时间" width="250" />
      <el-table-column fixed="right" label="操作" width="300">
        <template #default="scope">
          <el-button type="danger" round @click="deleteInfo(scope.row.id)"
            >删除</el-button
          >
          <el-button type="warning" round @click="editInfo(scope.row.id)"> 编辑</el-button>
          <el-button type="success" round @click="detailInfo(scope.row.id)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>


    <!-- 分页搜索 -->
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 5, 10, 15]"
      layout="jumper, total, sizes, prev, pager, next"
      background
      :total="pageTotal"
      @size-change="pageChange"
      @current-change="currentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />

    <!-- 编辑弹框-->
    <el-dialog v-model="dialogEdit" title="编辑">
      <el-form :model="dataEdit">
        <el-form-item label="id">
          <el-input v-model="dataEdit.id" autocomplete="off" disabled />
        </el-form-item>

        <el-form-item label="开始周">
          <el-input v-model="dataEdit.startWeek" autocomplete="off" />
        </el-form-item>

        <el-form-item label="结束周">
          <el-input v-model="dataEdit.endWeek" autocomplete="off" />
        </el-form-item>

        <el-form-item label="教师编号">
          <el-input v-model="dataEdit.teacherId" autocomplete="off" />
        </el-form-item>

        <el-form-item label="学分">
          <el-input v-model="dataEdit.credit" autocomplete="off" />
        </el-form-item>

        <el-form-item label="成绩">
          <el-input v-model="dataEdit.grade" autocomplete="off" />
        </el-form-item>

        <el-form-item label="科目编号">
          <el-input v-model="dataEdit.majorId" autocomplete="off" />
        </el-form-item>

        <el-form-item label="剩余容量">
          <el-input v-model="dataEdit.remain" autocomplete="off" />
        </el-form-item>

        <el-form-item label="状态">
          <el-input v-model="dataEdit.status" autocomplete="off" />
        </el-form-item>

        <el-form-item label="考试时间">
          <el-input v-model="dataEdit.examTime" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer="scope">
        <span class="dialog-footer">
          <el-button type="warning" round @click="dialogEdit = false">取消</el-button>
          <el-button type="success" round @click="editConfirm"> 确认 </el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 查看详情弹框 -->

    <el-dialog v-model="dialogDetail" title="详细信息" width="70%" top="5vh">
      <el-descriptions border class="px-32px" direction="vertical">
        <el-descriptions-item label="课程编号">{{ dataDetail?.id }}</el-descriptions-item>
        <el-descriptions-item label="课程名称">{{
          dataDetail?.courseName
        }}</el-descriptions-item>

        <el-descriptions-item label="开始周">{{
          dataDetail?.startWeek
        }}</el-descriptions-item>
        <el-descriptions-item label="结束周">{{
          dataDetail?.endWeek
        }}</el-descriptions-item>
        <el-descriptions-item label="教师id">{{
          dataDetail?.teacherId
        }}</el-descriptions-item>
        <el-descriptions-item label="学分">{{ dataDetail?.credit }}</el-descriptions-item>
        <el-descriptions-item label="成绩">{{ dataDetail?.grade }}</el-descriptions-item>
        <el-descriptions-item label="课本编号">{{
          dataDetail?.majorId
        }}</el-descriptions-item>
        <el-descriptions-item label="剩余容量">{{
          dataDetail?.remain
        }}</el-descriptions-item>
        <el-descriptions-item label="状态">{{ dataDetail?.status }}</el-descriptions-item>
        <el-descriptions-item label="考试时间">{{
          dataDetail?.examTime
        }}</el-descriptions-item>
      </el-descriptions>
    </el-dialog>

    <!-- 添加课程信息 -->
    <el-dialog v-model="dialogAdd" title="编辑">
      <el-form :model="dataAdd">
        <el-form-item label="课程名称">
          <el-input v-model="dataAdd.courseName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="开始周">
          <el-input v-model="dataAdd.startWeek" autocomplete="off" />
        </el-form-item>

        <el-form-item label="结束周">
          <el-input v-model="dataAdd.endWeek" autocomplete="off" />
        </el-form-item>

        <el-form-item label="教师编号">
          <el-input v-model="dataAdd.teacherId" autocomplete="off" />
        </el-form-item>

        <el-form-item label="学分">
          <el-input v-model="dataAdd.credit" autocomplete="off" />
        </el-form-item>

        <el-form-item label="成绩">
          <el-input v-model="dataAdd.grade" autocomplete="off" />
        </el-form-item>

        <el-form-item label="科目编号">
          <el-input v-model="dataAdd.majorId" autocomplete="off" />
        </el-form-item>

        <el-form-item label="剩余容量">
          <el-input v-model="dataAdd.remain" autocomplete="off" />
        </el-form-item>

        <el-form-item label="状态">
          <el-input v-model="dataAdd.status" autocomplete="off" />
        </el-form-item>

        <!-- <el-form-item label="考试时间">
          <el-input v-model="dataAdd.examTime" autocomplete="off" />
        </el-form-item> -->
      </el-form>
      <template #footer="scope">
        <span class="dialog-footer">
          <el-button type="warning" round @click="dialogAdd = false">取消</el-button>
          <el-button type="success" round @click="addConfirm"> 确认 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import {
  getAllCourseInfo,
  deleteByCourseId,
  getDetailByCourseId,
  updateCourseById,
  deleteCourseById,
  addCourseInfo,
} from "@/api/course.js";
import { ElMessage } from "element-plus";
import { onMounted, ref, reactive } from "vue";
import { ElMessageBox } from "element-plus";

const dialogEdit = ref(false);
const dialogDetail = ref(false);
const dialogAdd = ref(false);

const currentPage = ref(1);
const pageSize = ref(3);
const courseName = ref("");
//总共页数
const pageTotal = ref(0);

const courseData = ref([]);
//编辑
const dataEdit = ref({
  id: 0,
  startWeek: 0,
  endWeek: 0,
  teacherId: 0,
  credit: 0,
  grade: 0,
  majorId: 0,
  remain: 0,
  status: 0,
  examTime: "",
});

//添加
const dataAdd = ref({
  id: 0,
  courseName: "",
  startWeek: 0,
  endWeek: 0,
  teacherId: 0,
  credit: 0,
  grade: 0,
  majorId: 0,
  remain: 0,
  status: 0,
});

//详情

const dataDetail = ref({
  id: 0,
  courseName: "",
  startWeek: 0,
  endWeek: 0,
  teacherId: 0,
  credit: 0,
  grade: 0,
  majorId: 0,
  remain: 0,
  status: 0,
  examTime: "",
});

//页数改变时
const pageChange = async (size) => {
  pageSize.value = size;
  getAllInfo();
};

//页数大小改变时
const currentChange = async (num) => {
  currentPage.value = num;
  console.log(10086);
  console.log(num);
  getAllInfo();
};

const resetQuery = () => {
  currentPage.value = 1;
  pageSize.value = 3;
  console.log(currentPage.value);
  getAllInfo();
};

//管理员添加课程信息
const addCourse = async () => {
  dialogAdd.value = true;
};
const addConfirm = async () => {
  let result = await addCourseInfo(dataAdd.value);
  dialogAdd.value = false;
  getAllInfo();
};

//管理员删除课程信息

//获取课程详情
const detailInfo = async (id) => {
  dialogDetail.value = true;
  let result = await getDetailByCourseId(id);
  dataDetail.value = result.data;
};

//编辑课程信息
const editInfo = async (id) => {
  dialogEdit.value = true;
  const result = await getDetailByCourseId(id);
  console.log(result.data);
  Object.assign(dataEdit.value, result.data);
};

//完成编辑
const editConfirm = async () => {
  console.log(22222);
  console.log();
  let result = await updateCourseById(dataEdit.value);
  dialogEdit.value = false;
  getAllInfo();
};

//删除课程信息
const deleteInfo = async (id) => {
  ElMessageBox.confirm("你确认要删除课程信息吗?", "温馨提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      //调用接口
      let result = await deleteByCourseId(id);
      ElMessage({
        type: "success",
        message: "删除成功",
      });
      //刷新列表
      getAllInfo();
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "用户取消了删除",
      });
    });
};

//获取所有信息
const getAllInfo = async () => {
  let params = {
    page: currentPage.value,
    pageSize: pageSize.value,
    courseName: courseName.value ? courseName.value : null,
  };
  let result = await getAllCourseInfo(params);
  console.log(result.data);
  courseData.value = result.data.records;
  pageTotal.value = result.data.total;
};

onMounted(() => {
  getAllInfo();
});
</script>
